/// <reference types="vitest" />

import type { UserConfig, ConfigEnv } from 'vite'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import { resolve } from 'path'
import vueJsx from '@vitejs/plugin-vue-jsx'
import dotenv from 'dotenv'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import mdPlugin from 'vite-plugin-markdown'
import vueI18n from '@intlify/vite-plugin-vue-i18n'
import mockDevServerPlugin from 'vite-plugin-mock-dev-server' 

// https://vitejs.dev/config/
export default defineConfig(({ mode }: ConfigEnv) => {
  const dotenvConfig = dotenv.config({ path: `./.env.${mode}` }).parsed
  return {
    base: dotenvConfig?.BUILD_PATH || '/',
    build: {
      outDir: dotenvConfig?.BUILD_OUT_DIR || 'dist',
      rollupOptions: {
        output: {
          manualChunks(id) {
            if (id.includes('node_modules')) {
              return id.toString().split('node_modules/')[1].split('/')[0].toString()
            }
          }
        }
      }
    },
    server: {
      port: 5140,
      proxy: {
        '^/api/': {
          target: 'http://192.168.31.66:9998', // 后台服务器地址
          // target: 'http://192.168.1.15:9998',
          changeOrigin: true /* 允许跨域 */
        },
        '/socket.io': {
          target: 'ws://localhost:3000',
          ws: true
        }
      },
      // vite启动后打开的页面地址
      open: '/'
    },
    resolve: {
      alias: {
        "@": resolve(process.cwd(), 'src'),
        "@base-comp": resolve(process.cwd(), 'src/components'),
        "@UI": resolve(process.cwd(), 'src/UI'),
        "@stores": resolve(process.cwd(), 'src/stores'),
        "@router": resolve(process.cwd(), 'src/router'),
        "@pages": resolve(process.cwd(), 'src/views'),
        "@hooks": resolve(process.cwd(), 'src/hooks'),
        "#types": resolve(process.cwd(), 'src/types'),
        "@apis": resolve(process.cwd(), 'src/service'),
        "@utils": resolve(process.cwd(), 'src/utils'),
      }
    },
    test: {
      // jest like test api
      globals: true,
      // 模拟dom环境
      environment: 'happy-dom',
      // 支持tsx,jsx
      transformMode: {
        web: [/.[tj]sx$/]
      }
    },
    plugins: [
      vue(),
      AutoImport({
        imports: [
          'vue',
          'vue-router',
          'pinia',
          {
            axios: [
              ['default', 'axios'] // import { default as axios } from 'axios',
            ]
          },
          {
            qs: [
              ['default', 'qs'] // import { default as axios } from 'axios',
            ]
          }
        ],
        dts: 'src/types/auto-import.d.ts' //生成全局引入的文件
      }),
      Components({
        // allow auto load markdown components under `./src/components/`
        extensions: ["vue", "md"],
        // allow auto import and register components used in markdown
        include: [/\.vue$/, /\.tsx$/, /\.vue\?vue/, /\.md$/],
        resolvers: [ElementPlusResolver()],
        dts: "src/types/auto-import.d.ts",
      }),
      // @ts-ignore
      mdPlugin.plugin({
        mode: ['html', 'vue'],
      }),
      createSvgIconsPlugin({
        // 指定需要缓存的图标文件夹
        iconDirs: [resolve(process.cwd(), 'src/icons/svg')],
        // 指定symbolId格式
        symbolId: 'icon-[dir]-[name]'
      }),
      vueJsx(),
      vueI18n({
        include: resolve(__dirname, './src/lang/**')
      }),
      mockDevServerPlugin()
    ],
    css: {
      preprocessorOptions: {
        scss: {
          // scss变量可以直接用
          additionalData: `@use "src/styles/variables.scss" as *;`
        }
      }
    },
    optimizeDeps: {
      include: [
        "vue",
        "vue-router",
        'vue-types',
        '@vueuse/core',
        'loadsh-es'
      ]
    }
  }
})
